﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QLVT.Config;
using QLVT.Entity;

namespace QLVT.DAO
{
    class NhanVienDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from NhanVien");
        }

        public NhanVien GetBymanv(String manv)
        {
            NhanVien nv = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from NhanVien where manv = '" + manv + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    nv = new NhanVien();
                    nv.Manv = dataReader.GetString(0);
                    nv.Holot = dataReader.GetString(1);
                    nv.Tennv = dataReader.GetString(2);
                    nv.Std = dataReader.GetString(3);
                    nv.Ngaysinh = dataReader.GetDateTime(4);
                    nv.GioiTinh = dataReader.GetBoolean(5);
                    nv.Socmnd = dataReader.GetString(6);
                    nv.NgayVaoLam = dataReader.GetDateTime(7);
                    nv.Luong = dataReader.GetFloat(8);
                    return nv;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
               
            }
            return nv;
        }

        public bool Insert(NhanVien nhanVien)
        {
            String sql = "INSERT INTO [QLVT].[dbo].[nhanvien]([manv],[holot],[tennv],[sdt],[ngaysinh],[gioitinh],[socmnd],[ngayvaolam],[luong])"
               + "VALUES('" + nhanVien.Manv + "','" + nhanVien.Holot + "','" + nhanVien.Tennv + "','" + nhanVien.Std + "','" + nhanVien.Ngaysinh.ToShortDateString() + "','" + nhanVien.GioiTinh + "','" + nhanVien.Socmnd + "','" + nhanVien.NgayVaoLam.ToShortDateString() + "'," + nhanVien.Luong + ")";
            return Update(sql);
        }
        public bool DeleteBymanv(String manv)
        {
            return Update("delete from NhanVien where manv = '" + manv + "'");
        }

        public bool UpdateBymanv(NhanVien nv)
        {
            String sql = "update NhanVien "
                 + " set holot = '" + nv.Holot.Trim() + "'"
                 + ", tennv = '" + nv.Tennv.Trim() + "'"
                 + ", sdt = '" + nv.Std.Trim() + "'"
                 + ", ngaysinh = '" + nv.Ngaysinh.ToShortDateString() + "'"
                 + ", gioitinh = '" + nv.GioiTinh + "'"
                 + ", socmnd = '" + nv.Socmnd.Trim() + "'"
                 + ", ngayvaolam = '" + nv.NgayVaoLam.ToShortDateString() + "'"
                 + ", luong = " + nv.Luong + ""
                 + " where manv = '" + nv.Manv.Trim() + "'";
            return Update(sql);
        }
    }
}
